/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import business.CarBookingBusiness;
import business.TourBookingBusiness;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.CarBooking;
import model.CarBookingDetail;

/**
 *
 * @author Nhang Con
 */
@ManagedBean
@RequestScoped
public class ManagerCarBookingBean {

    private List<CarBooking> listCarBooking;
    private List<CarBookingDetail> listCarBookingDetail;
    private CarBooking carbookings;
    private CarBookingBusiness carbookingbz;
    private List<Integer> pageIndex;
    private int pageID;

    public CarBooking getCarbookings() {
        return carbookings;
    }

    public void setCarbookings(CarBooking carbookings) {
        this.carbookings = carbookings;
    }

    public List<CarBooking> getListCarBooking() {
        return listCarBooking;
    }

    public void setListCarBooking(List<CarBooking> listCarBooking) {
        this.listCarBooking = listCarBooking;
    }

    public int getPageID() {
        return pageID;
    }

    public void setPageID(int pageID) {
        this.pageID = pageID;
    }

    public List<Integer> getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(List<Integer> pageIndex) {
        this.pageIndex = pageIndex;
    }

    public List<CarBookingDetail> getListCarBookingDetail() {
        return listCarBookingDetail;
    }

    public void setListCarBookingDetail(List<CarBookingDetail> listCarBookingDetail) {
        this.listCarBookingDetail = listCarBookingDetail;
    }

    /** Creates a new instance of ManagerCarBookingBean */
    public ManagerCarBookingBean() {
        carbookingbz = new CarBookingBusiness();
        listCarBookingDetail = new ArrayList<CarBookingDetail>();
        TourBookingBusiness tbookbusiness = new TourBookingBusiness();
        if (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID1") != null) {
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID1").toString());
        } else {
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("PageID2").toString());
        }


        int index = tbookbusiness.getRowInListTour();
        pageIndex = new ArrayList<Integer>();

        if (index / 3 < 1) {
            pageIndex.add(1);
        } else {
            if (index % 3 == 0) {
                for (int i = 1; i <= index / 3; i++) {
                    pageIndex.add(i);
                }
            } else {
                for (int i = 1; i <= index / 3 + 1; i++) {
                    pageIndex.add(i);
                }
            }
        }
        listCarBooking = carbookingbz.getListBookingCar(pageID);
        CarBookingDetail carbookingdetail;
        for (CarBooking carBooking : listCarBooking) {
            carbookingdetail = new CarBookingDetail();
            carbookingdetail.setCarbookings(carBooking);
            carbookingdetail.setName(tbookbusiness.getUserNameFromID(carBooking.getCustomerID()));
            carbookingdetail.setCarname(carbookingbz.getCarNameByID(carBooking.getCarID()));
            listCarBookingDetail.add(carbookingdetail);
        }
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("PageID2", pageID);
    }

    public String cancelCarBooking(CarBooking carbooking) {

        if (carbookingbz.updateStatusBooking(carbooking)) {

            return "managercarbooking.xhtml";

        }
        return "errol.xhtml";


    }
}
